import 'dart:math';
import 'package:dio/dio.dart';

class AppException implements Exception {
  final String _message;
  final int _code;

  AppException(this._code, this._message);

  String toString(){
    return "$_code$_message";
  }

  // ignore: missing_return
  factory AppException.create(DioError error){
//    print("22222222222 = ${error.response.statusCode}");

    switch (error.type){
      case DioErrorType.CANCEL:
        {
          return BadRequestException(-1, "请求取消");
        }
        break;
      case DioErrorType.CONNECT_TIMEOUT:
        {
          return BadRequestException(-1, "连接超时");
        }
        break;
      case DioErrorType.SEND_TIMEOUT:
        {
          return BadRequestException(-1, "请求超时");
        }
        break;
      case DioErrorType.RECEIVE_TIMEOUT:
        {
          return BadRequestException(-1, "响应超时");
        }
        break;
      case DioErrorType.RESPONSE:
        {
          try {
            int errorCode = error.response.statusCode;

            switch(errorCode){
              case 1001:
                {
                  return BadRequestException(errorCode, "请求语法错误");
                }
                break;
              case 401:
                {
                  return UnauthorisedException(errorCode, "没有权限");
                }
                break;
              case 403:
                {
                  return UnauthorisedException(errorCode, "服务器拒绝执行");
                }
                break;
              case 404:
                {
                  return UnauthorisedException(errorCode, "无法连接服务器");
                }
                break;
              case 405:
                {
                  return UnauthorisedException(errorCode, "请求方法被禁止");
                }
                break;
              case 500:
                {
                  return UnauthorisedException(errorCode, "服务器内部错误");
                }
                break;
              case 502:
                {
                  return UnauthorisedException(errorCode, "无效的请求");
                }
                break;
              case 503:
                {
                  return UnauthorisedException(errorCode, "服务器报错");
                }
                break;
              case 505:
                {
                  return UnauthorisedException(errorCode, "不支持HTTP协议请求");
                }
                break;
              default:
                {
                  return AppException(errorCode, error.response.statusMessage);
                }
            }

          } on Exception catch(_){
            return AppException(-1, "未知错误");
          }
        }
        break;
      default:
        {
          return AppException(-1, error.message);
        }

    }
  }
}




/// 请求错
class BadRequestException extends AppException {
  BadRequestException([int code, String message]) : super(code, message);
}

/// 未认证异常
class UnauthorisedException extends AppException {
  UnauthorisedException(int code, String message) : super(code, message);
}